package cn.mdx.service;


import cn.mdx.common.beans.User;
import cn.mdx.common.exceptions.CheckException;
import cn.mdx.common.utils.CheckUtil;
import cn.mdx.common.utils.InternationalCheckUtil;
import cn.mdx.common.utils.FileUtil;
import cn.mdx.mapper.*;
import cn.mdx.pojo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;

/**
 * @author 谢模全
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ResourceService {

    private static final Logger logger = LoggerFactory.getLogger(ResourceService.class);

    @Autowired
    private ResourceMapper resourceMapper;

    @Autowired
    private ServiceMapper serviceMapper;

    @Autowired
    private PatentMapper patentMapper;

    @Autowired
    private SchoolMapper schoolMapper;

    @Autowired
    private SoftwareMapper softwareMapper;

    @Autowired
    private TeamMapper teamMapper;

    @Autowired
    private TerraceMapper terraceMapper;

    @Autowired
    private ResultMapper resultMapper;

    /**
     *
     * @return 所有资源类别
     */
    public ArrayList<Resource> getResources(){
        logger.info("[查询所有资源类别]");
        return resourceMapper.getResources();
    }

    /**
     *
     * @param uid 用户id
     * @return 该用户的所有服务Id
     */
    public ArrayList<Integer> getServicesId(Integer uid) {
        CheckUtil.notNull(uid,"参数不为空");
        logger.info("[查询id为<{}>的所有服务Id]",uid);
        return serviceMapper.getServicesId(uid);
    }


//----------------------------------以下为删除相关资源-------------------

    /**
     *
     * @param sid 服务资源id
     * @param uid 用户id
     */
    public void delService(Integer sid,HttpSession session, Integer uid) {
        CheckUtil.notNull(sid,"参数不为空");
        CheckUtil.notNull(uid,"用户id为空");
        logger.info("[删除服务资源中]");

        cn.mdx.pojo.Service service = serviceMapper.selectByPrimaryKey(sid);
        //控制删除人员为技术方自己和管理人员
        if(service == null || (!service.getUid().equals(uid))) {
            logger.info("[删除服务资源失败]");
            CheckUtil.fail("非法访问");
        }
        if(resultMapper.findResultBySid(sid)!=null){
            logger.info("[删除服务资源失败]");
            CheckUtil.fail("改资源已对接，不可删除");
        }
        switch (service.getRid()){
            case 1: delPatent(service.getIds(),session);break;
            case 2: delSchool(service.getIds(),session);break;
            case 3: delSoftware(service.getIds(),session);break;
            case 4: delTeam(service.getIds(),session);break;
            case 5: delTerrace(service.getIds(),session);break;
            default:
        }
        serviceMapper.deleteByPrimaryKey(sid);
        logger.info("[删除服务资源成功]");
    }

    /**
     *
     * @param ids 专利资源id
     * @param session HttpSession
     */
    private void delPatent(Integer ids, HttpSession session) {
        CheckUtil.notNull(ids,"参数不为空");
        logger.info("[删除专利资源中，参数[ids:{}]]",ids);
        String img = patentMapper.selectByPrimaryKey(ids).getImg();
        patentMapper.deleteByPrimaryKey(ids);
        FileUtil.deleteFile(session,img);
        logger.info("[删除完成]");
    }

    /**
     *
     * @param ids 校企资源id
     * @param session HttpSession
     */
    private void delSchool(Integer ids, HttpSession session) {
        CheckUtil.notNull(ids,"参数不为空");
        logger.info("[删除校企资源中，参数[ids:{}]]",ids);
        String img = schoolMapper.selectByPrimaryKey(ids).getImg();
        schoolMapper.deleteByPrimaryKey(ids);
        FileUtil.deleteFile(session,img);
        logger.info("[删除完成]");
    }

    /**
     *
     * @param ids 软著资源id
     * @param session HttpSession
     */
    private void delSoftware(Integer ids, HttpSession session) {
        CheckUtil.notNull(ids,"参数不为空");
        logger.info("[删除软著资源中，参数[ids:{}]]",ids);
        String img = softwareMapper.selectByPrimaryKey(ids).getImg();
        softwareMapper.deleteByPrimaryKey(ids);
        FileUtil.deleteFile(session,img);
        logger.info("[删除完成]");
    }

    /**
     *
     * @param ids 团队资源id
     * @param session HttpSession
     */
    private void delTeam(Integer ids, HttpSession session) {
        CheckUtil.notNull(ids,"参数不为空");
        logger.info("[删除团队资源中，参数[ids:{}]]",ids);
        String img = teamMapper.selectByPrimaryKey(ids).getImg();
        teamMapper.deleteByPrimaryKey(ids);
        FileUtil.deleteFile(session,img);
        logger.info("[删除完成]");
    }

    /**
     *
     * @param ids 平台资源id
     * @param session HttpSession
     */
    private void delTerrace(Integer ids, HttpSession session) {
        CheckUtil.notNull(ids,"参数不为空");
        logger.info("[删除平台资源中，参数[ids:{}]]",ids);
        String img = terraceMapper.selectByPrimaryKey(ids).getImg();
        terraceMapper.deleteByPrimaryKey(ids);
        FileUtil.deleteFile(session,img);
        logger.info("[删除完成]");
    }


//----------------------------------以下为添加相关资源-------------------


    /**
     *
     * @param patent 专利资源的参数
     * @param pic 图片文件
     * @throws IOException 上传文件
     */
    public void addPatent(Patent patent, MultipartFile pic, HttpSession session,Integer uid) throws IOException {
        CheckUtil.notNull(pic,"未上传图片");
        CheckUtil.notNull(uid,"用户id为空");
        //对象中参数arg中的三个属性必须为空,其他必须不为空
        CheckUtil.mustNullOtherOpposite(patent,"上传异常","img&pid&audit");
        logger.info("[添加专利资源中,参数[{}]]",patent.toString());

        String path = "img/3-"+uid+"/";
        String img = null;
        img = FileUtil.saveImgFile(session,pic,path);
        if (img!=null) {
            patent.setImg(img);
        }
        patentMapper.insert(patent);
        cn.mdx.pojo.Service service = new cn.mdx.pojo.Service();
        service.setIds(patent.getPid());
        service.setRid(1);
        service.setUid(uid);
        serviceMapper.insertSelective(service);
        logger.info("[添加成功]");
    }

    /**
     *
     * @param school 校企资源
     * @param pic 图片
     * @throws IOException 上传文件
     */
    public void addSchool(School school, MultipartFile pic, HttpSession session, Integer uid) throws IOException {
        CheckUtil.notNull(pic,"未上传图片");
        CheckUtil.notNull(uid,"用户id为空");
        //对象中参数arg中的三个属性必须为空,其他必须不为空
        CheckUtil.mustNullOtherOpposite(school,"上传异常","img&sid&audit");
        logger.info("[添加校企资源中,参数[{}]]",school);
        String path = "img/3-"+uid+"/";
        String img = null;
        img = FileUtil.saveImgFile(session,pic,path);
        if (img!=null) {
            school.setImg(img);
        }
        schoolMapper.insert(school);
        cn.mdx.pojo.Service service = new cn.mdx.pojo.Service();
        service.setIds(school.getSid());
        service.setRid(2);
        service.setUid(uid);
        serviceMapper.insertSelective(service);
        logger.info("[添加成功]");
    }

    /**
     *
     * @param software 软著资源
     * @param pic 图片
     * @param session HttpSession
     * @throws IOException 上传文件
     */
    public void addSoftware(Software software, MultipartFile pic, HttpSession session, Integer uid) throws IOException {
        CheckUtil.notNull(pic,"未上传图片");
        CheckUtil.notNull(uid,"用户id为空");
        //对象中参数arg中的三个属性必须为空,其他必须不为空
        CheckUtil.mustNullOtherOpposite(software,"上传异常","img&sid&audit");
        logger.info("[添加软著资源中,参数[{}]]",software);
        String path = "img/3-"+uid+"/";
        String img = null;
        img = FileUtil.saveImgFile(session,pic,path);
        if (img!=null) {
            software.setImg(img);
        }
        softwareMapper.insert(software);
        cn.mdx.pojo.Service service = new cn.mdx.pojo.Service();
        service.setIds(software.getSid());
        service.setRid(3);
        service.setUid(uid);
        serviceMapper.insertSelective(service);
        logger.info("[添加成功]");
    }

    /**
     *
     * @param team 团队资源
     * @param pic 图片
     * @param session HttpSession
     * @throws IOException 上传文件
     */
    public void addTeam(Team team, MultipartFile pic, HttpSession session, Integer uid) throws IOException {
        CheckUtil.notNull(pic,"未上传图片");
        CheckUtil.notNull(uid,"用户id为空");
        //对象中参数arg中的三个属性必须为空,其他必须不为空
        CheckUtil.mustNullOtherOpposite(team,"上传异常","img&tid&audit");
        logger.info("[添加团队资源中,参数[{}]]",team);
        String path = "img/3-"+uid+"/";
        String img = null;
        img = FileUtil.saveImgFile(session,pic,path);
        if (img!=null) {
            team.setImg(img);
        }
        teamMapper.insert(team);
        cn.mdx.pojo.Service service = new cn.mdx.pojo.Service();
        service.setIds(team.getTid());
        service.setRid(4);
        service.setUid(uid);
        serviceMapper.insertSelective(service);
        logger.info("[添加成功]");
    }

    /**
     *
     * @param terrace 平台资源
     * @param pic 图片
     * @param session HttpSession
     * @throws IOException 上传文件
     */
    public void addTerrace(Terrace terrace, MultipartFile pic, HttpSession session, Integer uid) throws IOException {
        CheckUtil.notNull(pic,"未上传图片");
        CheckUtil.notNull(uid,"用户id为空");
        //对象中参数arg中的三个属性必须为空,其他必须不为空
        CheckUtil.mustNullOtherOpposite(terrace,"上传异常","img&tid&audit");
        logger.info("[添加平台资源中,参数[{}]]",terrace);
        String path = "img/3-"+uid+"/";
        String img = null;
        img = FileUtil.saveImgFile(session,pic,path);
        if (img!=null) {
            terrace.setImg(img);
        }
        terraceMapper.insert(terrace);
        cn.mdx.pojo.Service service = new cn.mdx.pojo.Service();
        service.setIds(terrace.getTid());
        service.setRid(5);
        service.setUid(uid);
        serviceMapper.insertSelective(service);
        logger.info("[添加成功]");
    }


//----------------------------------以下为查询相关资源-------------------



    /**
     *
     * @param pid 专利资源id
     * @param uid 用户id
     * @return 一条专利资源详情
     */
    public Patent getPatent(Integer pid, Integer uid) {
        CheckUtil.notNull(uid,"用户id为空");
        CheckUtil.notNull(pid,"查询异常，参数为空");
        logger.info("[自我查询id为<{}>的专利资源中，]",pid);

        cn.mdx.pojo.Service service = new cn.mdx.pojo.Service();
        service.setUid(uid);
        service.setIds(pid);
        service.setRid(1);
        //根据用户id，资源分类和对应类型资源id查找总资源id
        Integer id = serviceMapper.findPrimaryKey(service);
        //查找不到，则为异常访问
        if(id != null) {
            Patent patent = patentMapper.selectByPrimaryKey(pid);
            patent.setPid(id);
            logger.info("[查询成功]");
            return patent;
        } else {
            logger.info("[查询失败]");
            CheckUtil.fail("非法访问");
            return null;
        }
    }


    /**
     *
     * @param sid 校企资源id
     * @param uid 用户id
     * @return 一条校企资源详情
     */
    public School getSchool(Integer sid, Integer uid) {
        CheckUtil.notNull(uid,"用户id为空");
        CheckUtil.notNull(sid,"查询异常，参数为空");
        logger.info("[自我查询id为<{}>的校企资源中，]",sid);
        cn.mdx.pojo.Service service = new cn.mdx.pojo.Service();
        service.setUid(uid);
        service.setIds(sid);
        service.setRid(2);
        Integer id = serviceMapper.findPrimaryKey(service);
        if(id != null) {
            School school = schoolMapper.selectByPrimaryKey(sid);
            school.setSid(id);
            logger.info("[查询成功]");
            return school;
        } else {
            logger.info("[查询失败]");
            CheckUtil.fail("非法访问");
            return null;
        }
    }

    /**
     *
     * @param sid 软著资源id
     * @param uid 用户id
     * @return 一条软著资源详情
     */
    public Software getSoftware(Integer sid, Integer uid) {
        CheckUtil.notNull(uid,"用户id为空");
        CheckUtil.notNull(sid,"查询异常，参数为空");
        logger.info("[自我查询id为<{}>的软著资源中，]",sid);
        cn.mdx.pojo.Service service = new cn.mdx.pojo.Service();
        service.setUid(uid);
        service.setIds(sid);
        service.setRid(3);
        Integer id = serviceMapper.findPrimaryKey(service);
        if(id != null) {
            Software software = softwareMapper.selectByPrimaryKey(sid);
            software.setSid(id);
            logger.info("[查询成功]");
            return software;
        } else {
            logger.info("[查询失败]");
            CheckUtil.fail("非法访问");
            return null;
        }
    }

    /**
     *
     * @param tid 团队资源id
     * @return 一条团队资源详情
     */
    public Team getTeam(Integer tid, Integer uid) {
        CheckUtil.notNull(uid,"用户id为空");
        CheckUtil.notNull(tid,"查询异常，参数为空");
        logger.info("[自我查询id为<{}>的团队资源中，]",tid);
        cn.mdx.pojo.Service service = new cn.mdx.pojo.Service();
        service.setUid(uid);
        service.setIds(tid);
        service.setRid(4);
        Integer id = serviceMapper.findPrimaryKey(service);
        if(id != null) {
            Team team = teamMapper.selectByPrimaryKey(tid);
            team.setTid(id);
            logger.info("[查询成功]");
            return team;
        } else {
            logger.info("[查询失败]");
            CheckUtil.fail("非法访问");
            return null;
        }
    }

    /**
     *
     * @param tid 平台资源id
     * @param uid 用户id
     * @return 一条平台资源详情
     */
    public Terrace getTerrace(Integer tid, Integer uid) {
        CheckUtil.notNull(uid,"用户id为空");
        CheckUtil.notNull(tid,"查询异常，参数为空");
        logger.info("[自我查询id为<{}>的平台资源中，]",tid);
        cn.mdx.pojo.Service service = new cn.mdx.pojo.Service();
        service.setUid(uid);
        service.setIds(tid);
        service.setRid(5);
        Integer id = serviceMapper.findPrimaryKey(service);
        if(id != null) {
            Terrace terrace = terraceMapper.selectByPrimaryKey(tid);
            terrace.setTid(id);
            logger.info("[查询成功]");
            return terrace;
        } else {
            logger.info("[查询失败]");
            CheckUtil.fail("非法访问");
            return null;
        }
    }

    /**
     *
     * @param id 专利资源id
     * @param delNoAudit 是否过滤未审核的
     * @return 专利资源信息
     */
    public Patent getAuditPatent(Integer id,Boolean delNoAudit){
        CheckUtil.notNull(id,"参数为空");
        CheckUtil.notNull(delNoAudit,"参数为空");
        logger.info("[获取专利资源中，参数[id:{},delNoAudit:{}]]");
        cn.mdx.pojo.Service service = new cn.mdx.pojo.Service();
        service.setIds(id);
        service.setRid(1);
        Integer sid = serviceMapper.findPrimaryKey(service);
        if(sid != null) {
            Patent obj = patentMapper.selectByPrimaryKey(id);
            if(delNoAudit && obj.getAudit()==0){
                logger.info("[查询失败]");
                CheckUtil.fail("非法访问");
            }
            obj.setPid(sid);
            logger.info("[查询成功]");
            return obj;
        } else {
            logger.info("[查询失败]");
            CheckUtil.fail("非法访问");
            return null;
        }
    }

    public School getAuditSchool(Integer id,Boolean delNoAudit){
        CheckUtil.notNull(id,"参数为空");
        CheckUtil.notNull(delNoAudit,"参数为空");
        logger.info("[获取校企资源中，参数[id:{},delNoAudit:{}]]");
        cn.mdx.pojo.Service service = new cn.mdx.pojo.Service();
        service.setIds(id);
        service.setRid(2);
        Integer sid = serviceMapper.findPrimaryKey(service);
        if(sid != null) {
            School obj = schoolMapper.selectByPrimaryKey(id);
            if(delNoAudit && obj.getAudit()==0){
                logger.info("[查询失败]");
                CheckUtil.fail("非法访问");
            }
            obj.setSid(sid);
            logger.info("[查询成功]");
            return obj;
        } else {
            logger.info("[查询失败]");
            CheckUtil.fail("非法访问");
            return null;
        }
    }

    public Software getAuditSoftware(Integer id,Boolean delNoAudit){
        CheckUtil.notNull(id,"参数为空");
        CheckUtil.notNull(delNoAudit,"参数为空");
        logger.info("[获取软著资源中，参数[id:{},delNoAudit:{}]]");
        cn.mdx.pojo.Service service = new cn.mdx.pojo.Service();
        service.setIds(id);
        service.setRid(3);
        Integer sid = serviceMapper.findPrimaryKey(service);
        if(sid != null) {
            Software obj = softwareMapper.selectByPrimaryKey(id);
            if(delNoAudit && obj.getAudit()==0){
                logger.info("[查询失败]");
                CheckUtil.fail("非法访问");
            }
            obj.setSid(sid);
            logger.info("[查询成功]");
            return obj;
        } else {
            logger.info("[查询失败]");
            CheckUtil.fail("非法访问");
            return null;
        }
    }

    public Team getAuditTeam(Integer id,Boolean delNoAudit){
        CheckUtil.notNull(id,"参数为空");
        CheckUtil.notNull(delNoAudit,"参数为空");
        logger.info("[获取团队资源中，参数[id:{},delNoAudit:{}]]");
        cn.mdx.pojo.Service service = new cn.mdx.pojo.Service();
        service.setIds(id);
        service.setRid(4);
        Integer sid = serviceMapper.findPrimaryKey(service);
        if(sid != null) {
            Team obj = teamMapper.selectByPrimaryKey(id);
            if(delNoAudit && obj.getAudit()==0){
                logger.info("[查询失败]");
                CheckUtil.fail("非法访问");
            }
            obj.setTid(sid);
            logger.info("[查询成功]");
            return obj;
        } else {
            logger.info("[查询失败]");
            CheckUtil.fail("非法访问");
            return null;
        }
    }

    public Terrace getAuditTerrace(Integer id,Boolean delNoAudit){
        CheckUtil.notNull(id,"参数为空");
        CheckUtil.notNull(delNoAudit,"参数为空");
        logger.info("[获取平台资源中，参数[id:{},delNoAudit:{}]]");
        cn.mdx.pojo.Service service = new cn.mdx.pojo.Service();
        service.setIds(id);
        service.setRid(5);
        Integer sid = serviceMapper.findPrimaryKey(service);
        if(sid != null) {
            Terrace obj = terraceMapper.selectByPrimaryKey(id);
            if(delNoAudit && obj.getAudit()==0){
                logger.info("[查询失败]");
                CheckUtil.fail("非法访问");
            }
            obj.setTid(sid);
            logger.info("[查询成功]");
            return obj;
        } else {
            logger.info("[查询失败]");
            CheckUtil.fail("非法访问");
            return null;
        }
    }









//----------------------------------以下为更新相关资源-------------------
    /**
     *
     * @param patent 专利资源的参数
     * @param pic 图片文件
     * @throws IOException 上传文件
     */
    public void setPatent(Patent patent, MultipartFile pic, HttpSession session,Integer uid) throws IOException {
        //对象中audit属性必须为空，其他随意
        CheckUtil.mustNull(patent,"数据异常","audit");
        CheckUtil.notNull(uid,"用户id为空");
        logger.info("[开始更新，参数[{}]]",patent);
        cn.mdx.pojo.Service service = serviceMapper.selectByPrimaryKey(patent.getPid());
        //控制更新人员为技术方自己和管理人员
        if(service==null || service.getRid()!= 1 || (!service.getUid().equals(uid))) {
            logger.info("[更新失败]");
            CheckUtil.fail("非法访问");
        }
        if(resultMapper.findResultBySid(patent.getPid())!=null){
            logger.info("[修改服务资源失败]");
            CheckUtil.fail("改资源已对接，不可修改");
        }
        patent.setPid(service.getIds());
        String path = "img/3-"+uid+"/";
        String img = FileUtil.saveImgFile(session,pic,path);
        String delImg = null;
        if(img!=null) {
            delImg = patent.getImg();
            patent.setImg(img);
        }
        patent.setAudit((byte) 0);
        patentMapper.updateByPrimaryKeySelective(patent);
        FileUtil.deleteFile(session,delImg);
        logger.info("[更新成功]");
    }

    /**
     *
     * @param school 校企资源
     * @param pic 图片
     * @throws IOException 上传文件
     */
    public void setSchool(School school, MultipartFile pic, HttpSession session, Integer uid) throws IOException {
        CheckUtil.mustNull(school,"数据异常","audit");
        CheckUtil.notNull(uid,"用户id为空");
        logger.info("[开始更新，参数[{}]]",school);
        cn.mdx.pojo.Service service = serviceMapper.selectByPrimaryKey(school.getSid());
        if(service==null || service.getRid()!= 2 || (!service.getUid().equals(uid))) {
            logger.info("[更新失败]");
            CheckUtil.fail("非法访问");
        }
        if(resultMapper.findResultBySid(school.getSid())!=null){
            logger.info("[修改服务资源失败]");
            CheckUtil.fail("改资源已对接，不可修改");
        }
        school.setSid(service.getIds());
        String path = "img/3-"+uid+"/";
        String img = FileUtil.saveImgFile(session,pic,path);
        String delImg = null;
        if(img!=null) {
            delImg = school.getImg();
            school.setImg(img);
        }
        school.setAudit((byte)0);
        schoolMapper.updateByPrimaryKeySelective(school);
        FileUtil.deleteFile(session,delImg);
    }

    /**
     *
     * @param software 软著资源
     * @param pic 图片
     * @param session HttpSession
     * @throws IOException 上传文件
     */
    public void setSoftware(Software software, MultipartFile pic, HttpSession session, Integer uid) throws IOException {
        CheckUtil.mustNull(software,"数据异常","audit");
        CheckUtil.notNull(uid,"用户id为空");
        logger.info("[开始更新，参数[{}]]",software);
        cn.mdx.pojo.Service service = serviceMapper.selectByPrimaryKey(software.getSid());
        if(service==null || service.getRid()!= 3 || (!service.getUid().equals(uid))) {
            logger.info("[更新失败]");
            CheckUtil.fail("非法访问");
        }
        if(resultMapper.findResultBySid(software.getSid())!=null){
            logger.info("[修改服务资源失败]");
            CheckUtil.fail("改资源已对接，不可修改");
        }
        software.setSid(service.getIds());
        String path = "img/3-"+uid+"/";
        String img = FileUtil.saveImgFile(session,pic,path);
        String delImg = null;
        if(img!=null) {
            delImg = software.getImg();
            software.setImg(img);
        }
        software.setAudit((byte)0);
        softwareMapper.updateByPrimaryKeySelective(software);
        FileUtil.deleteFile(session,delImg);
        logger.info("[更新成功]");
    }

    /**
     *
     * @param team 团队资源
     * @param pic 图片
     * @param session HttpSession
     * @throws IOException 上传文件
     */
    public void setTeam(Team team, MultipartFile pic, HttpSession session, Integer uid) throws IOException {
        CheckUtil.mustNull(team,"数据异常","audit");
        CheckUtil.notNull(uid,"用户id为空");
        logger.info("[开始更新，参数[{}]]",team);

        cn.mdx.pojo.Service service = serviceMapper.selectByPrimaryKey(team.getTid());
        if(service==null || service.getRid() != 4 || (!service.getUid().equals(uid))) {
            logger.info("[更新失败]");
            CheckUtil.fail("非法访问");
        }
        if(resultMapper.findResultBySid(team.getTid())!=null){
            logger.info("[修改服务资源失败]");
            CheckUtil.fail("改资源已对接，不可修改");
        }
        team.setTid(service.getIds());
        String path = "img/3-"+uid+"/";
        String img = FileUtil.saveImgFile(session,pic,path);
        String delImg = null;
        if(img!=null) {
            delImg = team.getImg();
            team.setImg(img);
        }
        team.setAudit((byte)0);
        teamMapper.updateByPrimaryKeySelective(team);
        FileUtil.deleteFile(session,delImg);
        logger.info("[更新成功]");
    }

    /**
     *
     * @param terrace 平台资源
     * @param pic 图片
     * @param session HttpSession
     * @throws IOException 上传文件
     */
    public void setTerrace(Terrace terrace, MultipartFile pic, HttpSession session, Integer uid) throws IOException {
        CheckUtil.mustNull(terrace,"数据异常","audit");
        CheckUtil.notNull(uid,"用户id为空");
        logger.info("[开始更新，参数[{}]]",terrace);
        cn.mdx.pojo.Service service = serviceMapper.selectByPrimaryKey(terrace.getTid());
        if(service==null || service.getRid() != 5 || (!service.getUid().equals(uid))) {
            logger.info("[更新失败]");
            CheckUtil.fail("非法访问");
        }
        if(resultMapper.findResultBySid(terrace.getTid())!=null){
            logger.info("[修改服务资源失败]");
            CheckUtil.fail("改资源已对接，不可修改");
        }
        terrace.setTid(service.getIds());
        String path = "img/3-"+uid+"/";
        String img = FileUtil.saveImgFile(session,pic,path);
        String delImg = null;
        if(img!=null) {
            delImg = terrace.getImg();
            terrace.setImg(img);
        }
        terrace.setAudit((byte)0);
        terraceMapper.updateByPrimaryKeySelective(terrace);
        FileUtil.deleteFile(session,delImg);
        logger.info("[更新成功]");
    }


}
